#!/bin/sh
##########################################################################
# This file is part of Hawkengine.
#
# Hawkengine is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Hawkengine is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Hawkengine.  If not, see <http://www.gnu.org/licenses/>.
##########################################################################

source $(dirname $0)/functions.sh

CreateHashCode()
{
  md5 -s $1 | tail -c 25 | tr [:lower:] [:upper:]
}

GetFileType()
{
case `echo ${file_name/*./}` in
c)
  echo "sourcecode.c.c";;
cpp)
  echo "sourcecode.cpp.cpp";;
m)
  echo "sourcecode.c.objc";;
png)
  echo "image.png";;
xib)
  echo "file.xib";;
*)
  echo "sourcecode";;
esac
}

#setup project.pbxproj
file_references=
file_reference_hashes=
build_files=
build_file_hashes=
for FILE in $IOS_SOURCES; do
  file_name=`basename $FILE`
  file_type=$(GetFileType $file_name)
  file_reference_hash=$(CreateHashCode $FILE)
  file_reference="$file_reference_hash = { isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = $file_type; name = $file_name; path = $FILE; sourceTree = SOURCE_ROOT; };"

  build_file_hash=$(CreateHashCode $file_reference)
  build_file="$build_file_hash = { isa = PBXBuildFile; fileRef = $file_reference_hash; };"

  file_references="$file_references $file_reference"
  file_reference_hashes="$file_reference_hash, $file_reference_hashes"
  build_files="$build_files $build_file"
  build_file_hashes="$build_file_hash, $build_file_hashes"
done
source_file_references=$file_references
source_file_reference_hashes=$file_reference_hashes
source_build_files=$build_files
source_build_file_hashes=$build_file_hashes

file_references=
file_reference_hashes=
build_files=
build_file_hashes=
for FILE in $IOS_RESOURCES; do
  file_name=`basename $FILE`
  file_type=$(GetFileType $file_name)
  file_reference_hash=$(CreateHashCode $FILE)
  file_reference="$file_reference_hash = { isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = $file_type; name = $file_name; path = $FILE; sourceTree = SOURCE_ROOT; };"

  build_file_hash=$(CreateHashCode $file_reference)
  build_file="$build_file_hash = { isa = PBXBuildFile; fileRef = $file_reference_hash; };"

  file_references="$file_references $file_reference"
  file_reference_hashes="$file_reference_hash, $file_reference_hashes"
  build_files="$build_files $build_file"
  build_file_hashes="$build_file_hash, $build_file_hashes"
done
resource_file_references=$file_references
resource_file_reference_hashes=$file_reference_hashes
resource_build_files=$build_files
resource_build_file_hashes=$build_file_hashes

defines=
for define in $IOS_DEFINES; do
  defines="$define, $defines"
done

includes=
for include in $IOS_INCLUDES; do
  includes="$include, $includes"
done

$(ReplaceInFile "HAWKENGINE_APP" "$IOS_APP" "$IOS_PROJECT_PBXPROJ")
$(ReplaceInFile "HAWKENGINE_DEFINES" "$defines" "$IOS_PROJECT_PBXPROJ")
$(ReplaceInFile "HAWKENGINE_INCLUDES" "$includes" "$IOS_PROJECT_PBXPROJ")
$(ReplaceInFile "HAWKENGINE_SOURCE_FILE_REFERENCES" "$source_file_references" "$IOS_PROJECT_PBXPROJ")
$(ReplaceInFile "HAWKENGINE_SOURCE_FILE_REFERENCE_HASHES" "$source_file_reference_hashes" "$IOS_PROJECT_PBXPROJ")
$(ReplaceInFile "HAWKENGINE_SOURCE_BUILD_FILES" "$source_build_files" "$IOS_PROJECT_PBXPROJ")
$(ReplaceInFile "HAWKENGINE_SOURCE_BUILD_FILE_HASHES" "$source_build_file_hashes" "$IOS_PROJECT_PBXPROJ")
$(ReplaceInFile "HAWKENGINE_RESOURCE_FILE_REFERENCES" "$resource_file_references" "$IOS_PROJECT_PBXPROJ")
$(ReplaceInFile "HAWKENGINE_RESOURCE_FILE_REFERENCE_HASHES" "$resource_file_reference_hashes" "$IOS_PROJECT_PBXPROJ")
$(ReplaceInFile "HAWKENGINE_RESOURCE_BUILD_FILES" "$resource_build_files" "$IOS_PROJECT_PBXPROJ")
$(ReplaceInFile "HAWKENGINE_RESOURCE_BUILD_FILE_HASHES" "$resource_build_file_hashes" "$IOS_PROJECT_PBXPROJ")
$(ReplaceInFile "HAWKENGINE_TMPDIR" "$IOS_TMPDIR" "$IOS_PROJECT_PBXPROJ")


#setup info.plist
icons=
for icon in $IOS_ICONS; do
  icons="<string>$icon</string> $icons"
done
$(ReplaceInFile "HAWKENGINE_VERSION" "$IOS_VERSION" "$IOS_INFO_PLIST")
$(ReplaceInFile "HAWKENGINE_TARGET" "$IOS_APP" "$IOS_INFO_PLIST")
$(ReplaceInFile "HAWKENGINE_DISPLAY_NAME" "$IOS_DISPLAY_NAME" "$IOS_INFO_PLIST")
$(ReplaceInFile "HAWKENGINE_ICONS" "$icons" "$IOS_INFO_PLIST")
$(ReplaceInFile "HAWKENGINE_BUNDLE" "$IOS_BUNDLE" "$IOS_INFO_PLIST")

echo
echo ios preparation for $IOS_APP complete.
echo

